﻿param 
(
    [string]$currentDirectory = $null
)

clear-host
write-host "------ RioLinx SharePoint Responsive Framework for SharePoint Foundation 2013 Standard Installation and Deployment -----"
write-host ""

#$executionPolicy = Get-ExecutionPolicy
#write-host "Current Execution Policy: " $executionPolicy
#Set-ExecutionPolicy bypass

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
  
 if ($snapin -eq $null) {
   Write-Host "Loading SharePoint Powershell Snap-in"
   Add-PSSnapin "Microsoft.SharePoint.Powershell"
 }

# Wait for timer job during deploy and retract
function Wait4TimerJob()
{
 $solution = Get-SPSolution | where-object {$_.Name -eq $solutionName}
 if ($solution -ne $null)
 {
  $counter = 1  
  $maximum = 50  
  $sleeptime = 2 
  
  Write-Host "Waiting to finish deployment solution timer job"
  while( ($solution.JobExists -eq $true ) -and ( $counter -lt $maximum ) )
  { 
   Write-Host "Please wait..."
   sleep $sleeptime 
   $counter++  
  }
  
  Write-Host "The solution has been deployed"   
 }
}


if($currentDirectory -eq $null) {
    $currentDirectory = Get-Location
}
$0 = $myInvocation.MyCommand.Definition
$env:dp0 = [System.IO.Path]::GetDirectoryName($0)

#$ScriptPath = Split-Path $MyInvocation.InvocationName
$scriptPath = "$env:dp0\DeploymentScript.ps1"
#write-host $scriptPath

$webapps = get-spwebapplication -IncludeCentralAdministration
foreach ($webapp in $webapps)
{
    if($webapp.IsAdministrationWebApplication) {
       $url = $webapp.url
       break
    }
}

write-host "Installing RioLinx SharePoint Responsive Framework for SharePoint Foundation 2013"


$solutionName = "RioLinx.SharePoint.Responsive.Foundation.wsp"
$logFileName = $solutionName + ".log"
$argumentList = "-solutionName:$solutionName -webAppUrl:$url -siteUrl:$url -webUrl:$url -logFileName:$logFileName -action:SD -directoryPath:$currentDirectory"
Invoke-Expression "& `"$scriptPath`" $argumentList"

Wait4TimerJob

#Set-ExecutionPolicy $executionPolicy

#Write-Host "Press any key to exit..."
#$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
